package me.xiaoyan.android.tuition.activity;

import java.util.Map;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import me.xiaoyan.android.tuition.R;
import me.xiaoyan.android.tuition.activity.core.ChildActivity;
import me.xiaoyan.android.tuition.utils.SESHttp;
import me.xiaoyan.android.widget.BaseUtil;
import me.xiaoyan.android.widget.DataBackListener;
import me.xiaoyan.android.widget.YanDialog;
import me.xiaoyan.json.JSONArray;
import me.xiaoyan.json.JSONObject;

public class Common extends ChildActivity {

	protected static final String GET_SCHOOLS = "tuitionFee/getSchools";
	protected static final String GET_STUDENT_PAYS = "tuitionFee/getDetail";
	protected static final String GET_TICKET = "echeck/getTicket";
	protected static final String CREATE_ORDER = "bill/cjdd";
	protected static final String GET_UNIONPAY_TN = "payment/hqzfbw";

	protected static final String USER_LOGIN = "customer/yhdl";
	protected static final String USER_REGISTER = "customer/yhzc";
	protected static final String SEND_SMSCODE = "verify/dxyzmhq";
	/**
	 * 商户号
	 */
	protected static final String MERCHANT_ID = "898421149000001";
	/**
	 * 业务编号
	 */
	protected static final String TRANSID = "10015";

	public static final String STUDENTS_PAYS = "StudentPays";
	public static final String PAY_INFO = "PayOrderInfo";
	public static final String ORDER_INFO = "MyOrders";
	public static final String CUSTOMER_ID = "Customer_Id";
	public static final String APP_DATA_KEY = "APP_DATA";
	public static final String APP_DATA_PHONE = "PHONE";
	public static final String APP_DATA_PASS = "PASS";
	public static final int LOGIN_SUCCESS_BACK = 10000;
	public static final int SIGUP_SUCCESS_BACK = 10000;
	protected static final int PAYMENT_ID = 1;
	protected static final String TANSURL = "tuitionFee/createBill";

	protected void setPageTitle(CharSequence title) {
		this.setTitle(title);
		if (findViewById(R.id.txt_app_header) != null) {
			((TextView) findViewById(R.id.txt_app_header)).setText(title);
		}
	}

	protected ProgressDialog getProgress(String text, boolean cancel) {
		ProgressDialog pd = ProgressDialog.show(this, "", text, true);
		pd.setCancelable(cancel);
		return pd;
	}

	protected void showBackButton() {
		if (null != findViewById(R.id.img_common_goback)) {
			findViewById(R.id.img_common_goback).setVisibility(View.VISIBLE);
			findViewById(R.id.img_common_goback).setOnClickListener(
					new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							goback();
						}
					});
		}

	}

	protected void clickGoto(int resId, final Class<?> activity) {
		if (null != findViewById(resId)) {
			clickGoto(findViewById(resId), activity);
		}
	}

	protected void clickGoto(View view, final Class<?> activity) {
		final Context c = this;
		view.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				c.startActivity(new Intent(c, activity));
			}
		});
	}

	protected void clickGoto(View view, final Intent intent) {
		final Context c = this;
		view.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				c.startActivity(intent);
			}
		});
	}

	protected void showToast(String text, int duration) {
		Toast.makeText(this, text, duration).show();
	}

	private long lastTime = 0;

	/**
	 * 程序退出提示
	 */
	protected void appexit() {
		long pressTime = BaseUtil.getTimestamp();
		if (pressTime - lastTime > 3) {
			lastTime = pressTime;
			showToast("再按一次退出程序", Toast.LENGTH_SHORT);
		} else {
			System.exit(0);
		}
	}

	protected JSONArray checkSESData(String source) throws Exception {
		JSONObject json = new JSONObject(source);
		if (json.optInt("code") != 0) {
			throw new Exception(json.getString("msg") + "("
					+ json.optInt("code") + ")");
		}
		return json.getJSONArray("data");
	}

	protected boolean checkTextBoxNull(TextView txt, int strRes) {
		return checkTextBoxNull(txt, getString(strRes));
	}

	protected boolean checkTextBoxNull(TextView txt, String message) {
		String value = txt.getText().toString();
		if (BaseUtil.stringIsEmpty(value)) {
			showToast(message, Toast.LENGTH_SHORT);
			return false;
		}
		return true;
	}

	protected void showReLoadData(final String url, final Map<String, Object> param,
			final DataBackListener dbl) {
		showDialog(this, getString(R.string.dialog_title_notice),
				getString(R.string.dialog_content_timeout))
				.setPositiveButton(R.string.app_btn_ok,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int arg1) {
								dialog.dismiss();
								SESHttp.sendRequest(url, param, dbl);
							}
						})
				.setNegativeButton("否", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int arg1) {
						dialog.dismiss();
					}
				}).show();
		;
	}

	protected static YanDialog showDialog(Context context, String title,
			String message) {
		YanDialog yd = new YanDialog(context);
		yd.setDTitle(title).setMessage(message);
		return yd;
	}
}
